package com.kadron.material;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.dao.KMaterialDAO;
import com.kadron.entity.Material;
import com.kadron.manager.KMessageManager;

@AutoCreate
@Name("kMaterialManager")
@Scope(ScopeType.PAGE)
public class KMaterialManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4184026689307942094L;

	private static final Log log = LogFactory.getLog(KMaterialManager.class);
	
	private Material material; 
	
	@In
	private KMaterialDAO materialDAO; 
	
	@Create
	public void init() {
		Long materialId = (Long) Component.getInstance("materialId");
		if (materialId == null) {
			materialId = new Long(0);
		}
		if (materialId > 0) {
			material = (Material) materialDAO.find(materialId, Material.class);
		} else {
			material = new Material();
		}
	}
	
	public String save() {
		if (material == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		if (log.isDebugEnabled()) {
			log.debug("Save Material materialId="+material.getId());
		}
		materialDAO.update(material);
		return "material";
	}

	public Material getMaterial() {
		return material;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}
}
